﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using EntityFramework.Extensions;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// GatherDal 的摘要说明。
    /// </summary>
    public class GatherDal
    {
        /// <summary>
        /// 添加采集规则
        /// </summary>
        /// <param name="model"></param>
        public void Add(Gather model)
        {
            using (Entities db = new Entities())
            {
                db.Gathers.Add(model);
                db.SaveChangesAsync().Wait();

            }
        }

        /// <summary>
        /// 修改采集规则
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Gather model)
        {
            using (Entities db = new Entities())
            {
                model.Category = null;
                db.Gathers.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }

        }
        /// <summary>
        /// 删除采集规则
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Gathers.Where(x => x.Id == id || x.PID == id);
                foreach (var v in q)
                {
                    db.Gathers.Remove(v);
                }
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 加载采集规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Gather GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                return db.Gathers.Include(x => x.Category).FirstOrDefault(x => x.Id == id);
            }
        }

        /// <summary>
        /// 复制采集任务
        /// </summary>
        /// <returns></returns>
        public IList<Gather> Copy()
        {
            using (Entities db = new Entities())
            {
                return db.Gathers.Where(x => x.PID == 0).ToArray();
            }
        }

        /// <summary>
        /// 绑定节点
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList<Gather> NodeBind(int pid)
        {
            using (Entities db = new Entities())
            {
                return db.Gathers.Where(x => x.PID == pid).ToArray();
            }
        }

        /// <summary>
        /// 采集规则
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<Gather> GetRules(int[] Ids, byte modelId)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Gathers
                        where !x.IsDisabled  && (db.Categoryes.Any(y => y.Id == x.CategoryId))
                        select x;
                if (Ids.Length > 0)
                {
                    q = q.Where(x => Ids.Contains(x.Id) || Ids.Contains(x.PID));
                }


                return q.OrderBy(x => x.PID).ThenBy(x => x.Id).ToArray();
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="keyWord"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Gather> BindList(byte modelId, string keyWord, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                var query = (from x in db.Gathers.Include(x => x.Category)
                             where x.PID == 0 && (db.Categoryes.Any(y => y.Id == x.CategoryId))

                             select x);

                if (string.IsNullOrEmpty(keyWord) == false)
                {
                    query = query.Where(x => x.Name.Contains(keyWord) || x.StartUrl.Contains(keyWord));
                }
                pp.RecordCount = query.Count();
                query = query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize);

                return query.ToArray();
            }
        }

        /// <summary>
        /// 最后一次工作时间
        /// </summary>
        /// <param name="id"></param>
        public void LastWork(int id)
        {
            using (Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand("UPDATE CMS_Gathers SET LastWork={1} WHERE Id={0}", id, DateTime.Now);
            }
        }
        /// <summary>
        /// 启用或者禁用
        /// </summary>
        /// <param name="id"></param>
        public void DisableOrUsedIt(int id)
        {
            using (Entities db = new Entities())
            {
                var infos = db.Gathers.Where(x => x.Id == id || x.PID == id);

                infos.ForEach(x => x.IsDisabled = !x.IsDisabled);
                db.SaveChangesAsync().Wait();
            }
        }
    }
}